package com.taotao.util;

import com.github.pagehelper.Page;
import com.taotao.common.Pagination;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;

import java.util.ArrayList;

/**
 * <p>
 *
 * </p>
 *
 * @author fanbeibei
 * @version 1.0.0
 * @date 2023/6/15 20:30
 */
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public class ConvertUtils {

    public static interface Convertor<T1, T2> {
        T2 convert(T1 t1);
    }


    public static <T1, T2> Pagination<T2> page2Pagination(Page<T1> page, Convertor<T1, T2> convertor) {

        Pagination<T2> pagination = new Pagination<>();

        pagination.setPageNum(page.getPageNum());
        pagination.setPageSize(page.getPageSize());
        pagination.setPages(page.getPages());
        pagination.setTotal(page.getTotal());
        pagination.setList(new ArrayList<>(page.getPageSize()));

        if (page.getTotal() > 0) {
            for (T1 t1 : page.getResult()) {
                pagination.getList().add(convertor.convert(t1));
            }
        }

        return pagination;

    }

}
